package com.loren.oauth.web.controller;

import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class AuthorizationController {

    @GetMapping("/oauth/error")
    public String errorPage(HttpServletRequest request) {
        Object errorObject = request.getAttribute("error");
        String errorMessage;
        if (errorObject instanceof OAuth2Exception) {
            OAuth2Exception oAuth2Exception = (OAuth2Exception) errorObject;
            errorMessage = oAuth2Exception.getMessage();
        } else {
            errorMessage = "unknown error";
        }
        request.setAttribute("errorMessage", errorMessage);
        return "error/error-page";
    }

}
